package com.xc.erp.modules.erp.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.common.utils.StringUtils;
import com.xc.erp.modules.erp.domain.dto.JshAccountParam;
import com.xc.erp.modules.erp.domain.pojo.JshAccount;
import com.xc.erp.modules.erp.service.JshAccountService;
import com.xc.erp.modules.erp.mapper.JshAccountMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.Objects;

/**
 *
 */
@Service
public class JshAccountServiceImpl extends ServiceImpl<JshAccountMapper, JshAccount>
    implements JshAccountService{

    @Override
    public IPage<JshAccount> listPageAccount(Integer page, Integer limit, JshAccountParam jshAccountParam) {
        IPage<JshAccount> iPage = new Page(page,limit);
        QueryWrapper<JshAccount> wrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(jshAccountParam.getName())) {
            wrapper.like("name",jshAccountParam.getName());
        }
        if (!StringUtils.isEmpty(jshAccountParam.getSerialNo())) {
            wrapper.eq("serial_no",jshAccountParam.getSerialNo());
        }
        if (!StringUtils.isEmpty(jshAccountParam.getRemark())) {
            wrapper.like("remark",jshAccountParam.getRemark());
        }
        wrapper.orderByAsc("sort");
        IPage<JshAccount> pageModel = baseMapper.selectPage(iPage, wrapper);
        return pageModel;
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public boolean batchUpdateStatus(boolean status, Long[] ids) {
        ArrayList<JshAccount> accounts = new ArrayList<>();
        for (Long id : ids) {
            JshAccount jshAccount = new JshAccount();
            jshAccount.setId(id);
            jshAccount.setEnabled(status);
            accounts.add(jshAccount);
        }
        return updateBatchById(accounts);
    }

    @Override
    public boolean checkAccountByName(String name) {
        JshAccount jshAccount = getOne(new QueryWrapper<JshAccount>().eq("name", name));
        if (Objects.isNull(jshAccount)) {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateByIsDefault(Long id, boolean isDefault) {
        UpdateWrapper<JshAccount> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("is_default",isDefault);
        return update(wrapper);
    }
}




